package com.example.test.advice.第十一章_开源世界.建议143_可以选择多种Collections扩展;

import ch.lambdaj.Lambda;
import gnu.trove.decorator.TIntListDecorator;
import gnu.trove.function.TIntFunction;
import gnu.trove.list.TIntList;
import gnu.trove.list.array.TIntArrayList;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.procedure.TIntProcedure;
import it.unimi.dsi.fastutil.BigList;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.objects.ObjectBigArrayBigList;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import java.util.ArrayList;
import java.util.List;

public class Client {

    public static void main(String[] args) {
        test();
        System.out.println("===================test2");
        test2();
        System.out.println("===================test3");
        test3();
    }

    //fastutil
    private static void test(){
        //明确键类型的Map
        Int2ObjectMap<String> map = new Int2ObjectOpenHashMap<>();
        map.put(100,"A");
        //超大容量的List，注意调整JVM的Heap内存
        //BigList<String> bigList = new ObjectBigArrayBigList<>(1L+Integer.MAX_VALUE);
        //基本类型的集合，不再使用Integer包装类型
        IntArrayList arrayList = new IntArrayList();
    }

    //Trove
    private static void test2(){
        //基本类型的集合，不再使用包装类型
        TIntList intList = new TIntArrayList();
        intList.add(2);

        //每个元素值乘以2
        intList.transformValues(new TIntFunction() {
            @Override
            public int execute(int i) {
                return i*2;
            }
        });
        System.out.println("initList:"+intList);
        intList.add(201);
        intList.add(300);
        //过滤，把大于200的元素组成一个新的列表
        TIntList t2 = intList.grep(new TIntProcedure() {
            @Override
            public boolean execute(int i) {
                return i>200;
            }
        });
        System.out.println("initList:"+intList);
        System.out.println("t2:"+t2);

        //包装为JDK的List
        List<Integer> list = new TIntListDecorator(intList);
        //键类型确定Map
        TIntObjectMap<String> map = new TIntObjectHashMap<>();
    }

    //lambdaj
    private static void test3(){
        List<Integer> ints = new ArrayList<>();
        ints.add(2);
        ints.add(3);
        ints.add(4);
        //计算平均值
        Number s = Lambda.avg(ints);
        System.out.println("平均值："+s);
        //按照年龄排序
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("张三",22));
        persons.add(new Person("李四",45));
        persons.add(new Person("王五",11));
        List<Person> sortPersonList = Lambda.sort(persons,Lambda.on(Person.class).getAge());
        System.out.println("原集合："+persons);
        System.out.println("排序："+sortPersonList);
        //串联所有元素的指定属性，输出为张三、李四、王五
        String name= Lambda.joinFrom(persons).getName();
        System.out.println("串联属性："+name);
        //过滤出年龄大于20岁的所有元素，输出为一个子列表
        List<Person> subPersonList = Lambda.select(persons, new BaseMatcher<Person>() {

            @Override
            public void describeTo(Description description) {

            }

            @Override
            public boolean matches(Object _person) {
                Person person = (Person) _person;
                return person.getAge()>20;
            }
        });
        System.out.println("原集合："+persons);
        System.out.println("过滤后子列表："+subPersonList);
        //找出最大年龄
        int maxAge = Lambda.maxFrom(persons).getAge();
        System.out.println("maxAge:"+maxAge);
        //抽出所有姓名形成一个数组
        List<String>list = Lambda.extract(persons,Lambda.on(Person.class).getName());
        System.out.println("list:"+list);
    }
}
